package com.aaa.web.service.impl.secondcarserviceimpl;

import com.aaa.entity.Employee;
import com.aaa.entity.secondcar.SecondCarDetailMessage;
import com.aaa.entity.secondcar.SecondCarRelatedMessage;
import com.aaa.entity.secondcar.SecondCarTransferMessage;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.SecondCarVerifyQuery;
import com.aaa.web.dao.secondcardao.SecondCarTransferDao;
import com.aaa.web.service.secondcarservice.SecondCarTransferService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @program: ${PROJECT_NAME}
* @description: 二手车过户界面
* @author: 许一鑫
* @create: 10:25 2019/1/3
**/
@Service
public class SecondCarTransferServiceImpl implements SecondCarTransferService {
    /**自动注入dao层*/
    @Autowired
    private SecondCarTransferDao dao;

    /**
     * 查询所有二手车相关信息展现在过户信息栏分页及搜索
     * @param query
     * @return
     */
    @Override
    public Map getCarTransferRelatedMessage(SecondCarVerifyQuery query) {
        int page=query.getPage();
        int size=query.getLimit();
        PageHelper.startPage(page,size);
        //获取登录员工
        Employee emp= EmpUtil.getEmp();
        query.setEmpschoolid(emp.getSchoolid());
        //存放返回信息的map
        Map m = new HashMap();
        List<SecondCarRelatedMessage> list = dao.getcartransferRelatedMessage(query);
        m.put("data",list);
        m.put("count",dao.getCarTransferRelatedCount());
        m.put("code",0);
        m.put("msg","");
        return m;
    }

    /**
     * 根据车主电话和车出厂码查询出需要过户的二手车信息
     * @param s
     * @return
     */
    @Override
    public List<SecondCarDetailMessage> getCarTransferDetailMessage(SecondCarDetailMessage s) {
        List<SecondCarDetailMessage> list=dao.getcartransferDetailMessage(s);
        return list;
    }
    /**根据原车主电话号码和新车主电话号码进行二手车过户*/
    @Override
    public Result transferCar(SecondCarTransferMessage s) {

        //获取员工schoolid
        Employee emp=EmpUtil.getEmp();
        //设置返回值
        Result result=new Result();

        //将用户信息表中的原车主的车辆出厂码修改为0
        dao.changeOldMasterCarcode(s);
        //将用户信息表中的新车主的车辆出厂码修改为该车
        dao.changeNewMasterCarcode(s);
        //将车辆总信息表中的用户号码修改为新用户的号码
        dao.changeCarMessageNewMaster(s);
        //将过户记录记录到二手车过户记录表
        dao.insertRecord(s);
        //将二手车市场的该贩售信息跟改为售出，二手市场的表中issell改为1
        dao.secondCarIssell(s);
        result.setCode(ISystemConst.SUCCESS_CODE);
        return result;
    }
}
